<?php

declare(strict_types=1);

namespace app\xapply\controller;

use app\xapply\middleware\Auth;
use app\xapply\Xapply;
use app\user\model\Department;
use app\user\model\Admin;

class Contact extends Xapply
{

    protected $middleware = [Auth::class];

    /**
     * 部门与成员
     * url: http://域名/xapply/contact/users
     * doc: http://lutui.net/doc/app/contacts.html
     */
    public function users()
    {
        $id = $this->data['id'];
        $arr = Department::where('pid', $id)->order(['sort' => 'desc', 'id' => 'asc'])->select();
        $data = [];
        foreach ($arr as $value) {
            $data[] = [
                'id' => intval($value['id']),
                'name' => $value['title'],
                'dept' => 1,
                'icon' => '',
            ];
        }
        if ($id > 0){
            $did = get_department_son($id);
            $arr = Admin::where('did', 'in', $did)->where('status', 1)->select();
        }else{
            $arr = Admin::where('status', 1)->select();
        }
        if ($arr){
            foreach ($arr as $value) {
                $thumb = $value['thumb'];
                if (!strstr($value['thumb'], 'http')){
                    $thumb = request()->domain().$value['thumb'];
                }
                $data[] = [
                    'id' => intval($value['id']),
                    'name' => $value['nickname'],
                    'phone' => $value['mobile'],
                    'url' => $thumb,
                ];
            }
        }
        return json($data);
    }
}
